package com.dotools.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

public class ChannelMgr {

    /**获取配置信息：分发渠道ID*/
    public static String getBranchLibID(Context ctx) {
        ApplicationInfo appInfo = null;
        try {
            appInfo = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
            Object obj = appInfo.metaData.get("ido_LIBID");
            if (obj instanceof Integer) {
                return appInfo.metaData.getInt("ido_LIBID") + "";
            } else if (obj instanceof String) {
                return appInfo.metaData.getString("ido_LIBID");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /**获取配置信息：分发渠道ID*/
    public static String getUmengChannel(Context ctx) {
        ApplicationInfo appInfo = null;
        try {
            appInfo = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
            Object obj = appInfo.metaData.get("UMENG_CHANNEL");
            if (obj instanceof Integer) {
                return appInfo.metaData.getInt("UMENG_CHANNEL") + "";
            } else if (obj instanceof String) {
                return appInfo.metaData.getString("UMENG_CHANNEL");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**获取配置信息：分发渠道ID*/
    public static String getUmengKey(Context ctx) {
        ApplicationInfo appInfo = null;
        try {
            appInfo = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
            Object obj = appInfo.metaData.get("UMENG_APPKEY");
            if (obj instanceof Integer) {
                return appInfo.metaData.getInt("UMENG_APPKEY") + "";
            } else if (obj instanceof String) {
                return appInfo.metaData.getString("UMENG_APPKEY");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    
    public static boolean is360(Context context){
        try {
            if("360".equals(getUmengChannel(context))){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static boolean isGooglePlay(Context context){
        try {
            if("googleplay".equals(getUmengChannel(context))){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /****
     *
     * @param marketPkg   市场包名
     * @param searchViewName  详细页Activity名称
     * @param searchPkg 要导量的apk包名
     * @param context   上下文
     * @return
     */
    public static boolean jumpMarket(String marketPkg,String searchViewName,String searchPkg,Context context){
        try {
            Intent it = context.getPackageManager().getLaunchIntentForPackage(marketPkg);
            it.setComponent(new ComponentName(marketPkg, searchViewName));
            it.setData(Uri.parse("market://details?id=" + searchPkg));
            context.startActivity(it);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    public static Intent getMarket(String marketPkg,String searchViewName,String searchPkg,Context context){
        Intent it  = null;
        try {
            it = context.getPackageManager().getLaunchIntentForPackage(marketPkg);
            it.setComponent(new ComponentName(marketPkg, searchViewName));
            it.setData(Uri.parse("market://details?id=" + searchPkg));
        } catch (Exception e) {
            return null;
        }
        return it;
    }
    
    public static void appRate(Context context) {
        Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (Exception e) {
            Toast.makeText(context, "Couldn't launch the market !",Toast.LENGTH_SHORT).show();
        }
    }
    
    public static void appRate(Context context,String pkg) {
        Uri uri = Uri.parse("market://details?id=" + pkg);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (Exception e) {
            Toast.makeText(context, "Couldn't launch the market !",Toast.LENGTH_SHORT).show();
        }
    }

    /****
     * 跳转OPPO市场详细APP页面
     * @param context
     * @param pkg  APP包名
     */
    public static void jump2OppoMarket(Context context,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("oppomarket://details?id="+pkg));
        context.startActivity(intent);
    }

    /****
     * 跳转小米市场详细APP页面
     * 小米手机会自动拦截market协议，跳转到自己的市场，非小米手机 此方法不可用
     * @param context
     * @param pkg  APP包名
     */
    public static void jump2XiaomiMarket(Context context,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id="+pkg));
        context.startActivity(intent);
    }

    /****
     * 跳转华为市场详细APP页面
     * @param context
     * @param pkg  APP包名
     */
    public static void jump2HuaweiMarket(Context context,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("appmarket://details?id="+pkg));
        context.startActivity(intent);
    }

    /****
     * 跳转VIVO市场详细APP页面
     * @param context
     * @param pkg  APP包名
     */
    public static void jump2VivoMarket(Context context,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("vivomarket://details?id="+pkg));
        context.startActivity(intent);
    }

    /****
     * 跳转魅族市场详细APP页面
     * @param context
     * @param pkg  APP包名
     */
    public static void jump2MeizuMarket(Context context,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("mstore://details?package_name="+pkg));
        context.startActivity(intent);
    }
}
